// 截取url后面字段
export const getUrlParams = function (key: any) {
    let url = window.location.href;
    const arr = url.split("?");
    const newArr = arr[1] ? arr[1].split("&") : [];
    for (var i = 0; i < newArr.length; i++) {
        let temp = newArr[i].split("=");
        if (temp[0] === key) {
            let search = temp[1];
            if (search.indexOf("#") > 0) {
                search = search.substring(0, search.indexOf("#"));
            }
            return search;
        }
    }
}
// 获取剩余时间
export const getlastTime = function () {
    let _nowTime = new Date().getTime();
    let week_time = new Date().getDay();
    let time = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000).getTime()
    let sec = Math.floor((time - _nowTime) / 1000) + (7 - week_time) * 86400
    let day: any = Math.floor(sec / 86400);
    let lasetTime = sec - (day * 86400)
    let hour: any = Math.floor(lasetTime / 3600);
    let minute: any = Math.floor((lasetTime - hour * 3600) / 60);
    let second: any = lasetTime - hour * 3600 - minute * 60;
    if (hour < 10) {
        hour = "0" + hour;
    }
    if (minute < 10) {
        minute = "0" + minute;
    }
    if (second < 10) {
        second = "0" + second;
    }
    hour = hour > 0 ? hour : 0
    minute = minute > 0 ? minute : 0
    second = second > 0 ? second : 0
    if (day > 0) {
        return day + "天" + hour + "时" + minute + "分" + second + "秒";
    } else {
        return hour + "时" + minute + "分" + second + "秒";

    }
}

// 调取客户端方法 -----------------------------------------------------------------
export const isIos = function () {
    return !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
}
export const isAndroid = function () {
    return (
        navigator.userAgent.indexOf("Android") > -1 ||
        navigator.userAgent.indexOf("Linux") > -1
    );
}

// 分享到微信 qq 微博
export const share = function (img: any, title: any, desc: any, url: any) {
    var data = {
        img: img,
        title: title,
        desc: desc,
        url: url
    };
    if (isIos()) {
        (window as any).webkit.messageHandlers.shareClicked.postMessage(data);
    } else if (isAndroid()) {
        keke.shareClicked(data.url, data.img, data.desc, data.title);
    }
}


// 跳转听听房间
export const goCommon = function (data: any) {
    if (isIos()) {
        (window as any).webkit.messageHandlers.onClickOpen.postMessage(data);
    } else if (isAndroid()) {
        keke.onClickOpen(JSON.stringify(data));
    }
}

// 跳转来来用户信息
export const goUserInfo = function (data: any) {
    if (isIos()) {
        (window as any).webkit.messageHandlers.onClickOpen.postMessage(data);
    } else if (isAndroid()) {
        keke.onClickOpen(JSON.stringify(data));
    }
}